package com.wing.device.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.CodeUtil;
import com.wing.device.dao.DeviceModelDao;
import com.wing.device.model.entity.DeviceBrand;
import com.wing.device.model.entity.DeviceModel;
import com.wing.device.model.request.DeviceModelForm;
import com.wing.device.model.response.DeviceModelVO;
import com.wing.factory.service.FacFactoryService;
import com.wing.product.dao.DynamicDao;
import com.wing.product.model.entity.ProductType;
import com.wing.product.service.ProductTypeService;
import com.wing.res.model.response.DeviceOrWorkVo;
import com.wing.web.excel.DeviceModelExcelIn;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 设备型号-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DeviceModelServiceImpl extends ServiceImpl<DeviceModelDao, DeviceModel> implements DeviceModelService {

    @Autowired
    private FacFactoryService facFactoryService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private DeviceBrandService deviceBrandService;

    @Autowired
    private DynamicDao dynamicDao;

    @Override
    @Transactional
    public int add(DeviceModelForm deviceModelForm) {

        if(Objects.nonNull(deviceModelForm.getFactoryId())){
            facFactoryService.get4appById(deviceModelForm.getFactoryId());
        }

        //校验

        DeviceModel deviceModel =new DeviceModel();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(DeviceModel.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(deviceModelForm, deviceModel, copyOptions);

        return baseMapper.insert(deviceModel);
    }

    @Override
    public List<DeviceModelVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<DeviceModelVO> pageList4app(Page page, Map<String, Object> params) {
        List<DeviceModelVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public DeviceModelVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    /**
     * 查询工种信息
     * @param
     * @return
     */
    @Override
    public List<DeviceOrWorkVo> fessionlist() {
        return baseMapper.fessionlist();
    }


    @Override
    public void importExcel(List<DeviceModelExcelIn> list) {
        //判断SKU编号是否有重复
        List newList = list.stream().map(DeviceModelExcelIn::getModelNo).distinct().collect(Collectors.toList());
        if(newList.size() != list.size()){
            throw new BusinessException("设备编码重复！");
        }
        int i = 1;
        for(DeviceModelExcelIn item:list){
            i++;
            if(StringUtils.isBlank(item.getName())){
                throw new BusinessException(300, "第"+i+"行设备名称未输入数据");
            }
            if(StringUtils.isBlank(item.getDeviceType())){
                throw new BusinessException(300, "第"+i+"行设备类型未输入数据");
            }
            if(StringUtils.isBlank(item.getDeviceBrand())){
                throw new BusinessException(300, "第"+i+"行设备品牌未输入数据");
            }
            if(StringUtils.isBlank(item.getModelNo())){
                throw new BusinessException(300, "第"+i+"行设备型号编码未输入数据");
            }
            DeviceModel deviceModel = new DeviceModel();
            deviceModel.setName(item.getName());
            deviceModel.setModelNo(item.getModelNo());
            deviceModel.setDeviceHead(item.getDeviceHead());
            deviceModel.setFactoryName(item.getFactoryName());
            deviceModel.setFactoryContact(item.getFactoryContact());
            deviceModel.setFactoryUrl(item.getFactoryUrl());
            deviceModel.setRemark(item.getRemark());
            // 设备类型
            if(StringUtils.isNotBlank(item.getDeviceType())){
                ProductType productType = productTypeService.getOne(new LambdaQueryWrapper<ProductType>().eq(ProductType::getName, item.getDeviceType())) ;
                if(productType!=null){
                    deviceModel.setDeviceType(productType.getId().toString());
                }
            }
            // 设备品牌
            if(StringUtils.isNotBlank(item.getDeviceBrand())){
                DeviceBrand deviceBrand = deviceBrandService.getOne(new LambdaQueryWrapper<DeviceBrand>().eq(DeviceBrand::getName, item.getDeviceBrand()));
                if(deviceBrand != null){
                    deviceModel.setDeviceBrand(deviceBrand.getId().toString());
                }else{
                    Integer maxNo = dynamicDao.getMaxNo("dev_device_brand");
                    String code = CodeUtil.getCode(maxNo);
                    DeviceBrand brand = new DeviceBrand();
                    brand.setCode("PP" + code);
                    brand.setNo(Integer.valueOf(code));
                    brand.setName(item.getDeviceBrand());
                    deviceBrandService.save(brand);
                    deviceModel.setDeviceBrand(deviceBrand.getId().toString());
                }
            }

            deviceModel.setCreator(JwtUtils.getLoginUserId());
            deviceModel.setCreatorName(JwtUtils.getLoginUsername());
            // 设备照片必传，但是没法获取
            deviceModel.setDevicePic(" ");
            baseMapper.insert(deviceModel);
        }
    }

    /**
     * 查询设备列表
     * @return
     */
    @Override
    public List<DeviceOrWorkVo> devicelist() {
        return baseMapper.devicelist();
    }
}
